/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cetc.iframe;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import com.cetc.dao.Dao;

public class BackupAndRestore extends javax.swing.JInternalFrame {

    /**
     * Creates new form BackupAndRestore
     */
    public BackupAndRestore() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jFrame1 = new javax.swing.JFrame();
        jMenu3 = new javax.swing.JMenu();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        backupTextField = new javax.swing.JTextField();
        backupButton = new javax.swing.JButton();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        restoreTextField = new javax.swing.JTextField();
        browseButton = new javax.swing.JButton();
        restoreButton = new javax.swing.JButton();

        jMenu1.setText("jMenu1");

        jMenu2.setText("jMenu2");

        jMenuItem1.setText("jMenuItem1");

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jMenu3.setText("jMenu3");

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("数据库备份与恢复");
        setPreferredSize(new java.awt.Dimension(600, 300));

        backupTextField.setText("数据库备份路径：");
		backupTextField.setEditable(false);// 设置 “数据库备份”文本框不可编辑
        backupTextField.setForeground(Color.BLUE);

        backupButton.setText("备份（K）");
        backupButton.setMnemonic(KeyEvent.VK_K);// 设置“备份”按钮的键盘助记符为K
		backupButton.addActionListener(new java.awt.event.ActionListener() {// “备份”按钮添加动作事件的监听
			public void actionPerformed(ActionEvent e) {
				try {
					String filePath = Dao.backup();// 获得备份sql文件的路径
					backupTextField.setText("数据库备份路径：" + filePath);// 设置“数据库备份”文本框中的文本内容
				} catch (Exception e1) {
					e1.printStackTrace();// 打印异常信息
					String message = e1.getMessage();// 获得全部异常信息
					int index = message.lastIndexOf(']');// 获得“]”在异常信息中最后一次出现处的索引
					message=message.substring(index+1);// 截串获得最后一次出现']'后的异常信息
					JOptionPane.showMessageDialog(BackupAndRestore.this, message);// 弹出异常信息提示框
					return;// 退出应用程序
				}
				JOptionPane.showMessageDialog(BackupAndRestore.this, "备份成功");// 弹出“备份成功”的提示框
			}
		});

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(67, 67, 67)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(backupButton)
                    .addComponent(backupTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 377, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(115, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(backupTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(backupButton)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("数据库备份", jPanel1);

        restoreTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                restoreTextFieldActionPerformed(evt);
            }
        });

        browseButton.setText("浏览（W）...");
        browseButton.setMnemonic(KeyEvent.VK_W);// 设置“浏览”按钮的键盘助记符为W
		browseButton.addActionListener(new java.awt.event.ActionListener() {// 为“浏览”按钮添加动作事件的监听
			public void actionPerformed(java.awt.event.ActionEvent e) {
				JFileChooser dirChooser=new JFileChooser("./backup/");// 把“./backup/”作为路径创建文件选择器
				int option = dirChooser.showOpenDialog(BackupAndRestore.this);// 获得“打开”、“取消”的返回值
				if(option==JFileChooser.APPROVE_OPTION){// 点击“打开”按钮
					File selFile = dirChooser.getSelectedFile();// 获得文件选择器中的文件
					restoreTextField.setText(selFile.getAbsolutePath());// 设置“数据库恢复”文本框中的文本内容
				}
			}
		});

        restoreButton.setText("恢复（R）");
        restoreButton.setMnemonic(KeyEvent.VK_R);// 设置“恢复”按钮的键盘助记符为R
		restoreButton.addActionListener(new java.awt.event.ActionListener() {// 为“恢复”按钮添加动作事件的监听
			public void actionPerformed(java.awt.event.ActionEvent e) {
				String path = restoreTextField.getText();// 获得“数据库恢复”文本框中的路径
				if(path==null||path.isEmpty())// 路径不存在或路径下没有文件
					return;// 退出应用程序
				File restoreFile=new File(path);// 根据路径创建文件对象
				restoreFile.getAbsolutePath();// 获得文件对象的绝对路径
				try {
					Dao.restore(restoreFile.getAbsolutePath());// 数据库恢复
				} catch (Exception e1) {
					e1.printStackTrace();// 打印异常信息
					String message = e1.getMessage();// 获得全部异常信息
					int index = message.lastIndexOf(']');// 获得“]”在异常信息中最后一次出现处的索引
					message=message.substring(index+1);// 截串获得最后一次出现']'后的异常信息
					JOptionPane.showMessageDialog(BackupAndRestore.this, message);// 弹出异常信息提示框
					return;// 退出应用程序
				}
				JOptionPane.showMessageDialog(BackupAndRestore.this, "恢复成功");// 弹出“恢复成功”的提示框
			}
		});

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(browseButton)
                        .addGap(14, 14, 14)
                        .addComponent(restoreButton))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addGap(65, 65, 65)
                        .addComponent(restoreTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 377, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(117, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(restoreTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(browseButton)
                    .addComponent(restoreButton))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        jTabbedPane2.addTab("数据库恢复", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTabbedPane2)
                    .addComponent(jTabbedPane1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane2)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void restoreTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_restoreTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_restoreTextFieldActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(BackupAndRestore.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BackupAndRestore.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BackupAndRestore.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BackupAndRestore.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BackupAndRestore().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backupButton;
    private javax.swing.JTextField backupTextField;
    private javax.swing.JButton browseButton;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JButton restoreButton;
    private javax.swing.JTextField restoreTextField;
    // End of variables declaration//GEN-END:variables
}
